package bitmap;

import java.io.*;
import java.lang.*;
import java.util.*;

import btree.LeafData;
import btree.ScanDeleteException;
import btree.ScanIteratorException;
import global.*;
import heap.*;
import btree.*;

public class BMFileScan extends IndexFileScan implements GlobalConst {
	
	String BMFilename;
	int position;
	boolean didfirst;
	boolean deletedcurrent;
	int keyType;
	int maxKeysize;
	int current_position = 0;
	byte iterator_value = 1;
	BitSet bitmap;
	KeyDataEntry key ;
	
	/**
	 * Iterate once (during a scan).  
	 *@return null if done; otherwise next position
	 *@exception ScanIteratorException iterator error
	 */

	public KeyDataEntry get_next() throws ScanIteratorException{
		try{
			

			int position = bitmap.nextSetBit(current_position);
			current_position = position + 1;
//			for(int i =0;i<bitmap.length();i++)
//			{
			key = new KeyDataEntry(position);
			return key;
		}catch(Exception ex){
			ex.printStackTrace();
			throw new ScanIteratorException();
		}
	}
//	public int getdata()
//	{
//		int position;
//		;
//		return position;
//	}
	
	 /** max size of the key
	   *@return the maxumum size of the key in BTFile
	   */
	  public int keysize() {
	    return maxKeysize;
	  }
	  
	  public void delete_current() throws ScanDeleteException
	  {
		  try{
			  if(bitmap.length() == 0){
				  System.out.println("BitMap File is empty");
				  throw new ScanDeleteException();
			  }
			  
			  if( (deletedcurrent == true) || (didfirst==false) ) 
				return;    
				     
			  // This position will be updated during the due course of development.
			  bitmap.set(current_position, false);
			  deletedcurrent = true;
			  return;
			  
		  }catch (Exception ex){
			  ex.printStackTrace();
			  throw new ScanDeleteException();
		  }
	  }
	  
	  public void intialize_bitset(){
		  bitmap = new BitSet();
		  // I need to implement the method of recreating the bitset from the bitmap file
	  }

}
